<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目" prop="projectNo">
        <el-select v-model="queryParams.projectNo" @change="getFunList" placeholder="请选择项目" clearable>
          <el-option
            v-for="p in projectList"
            :key="p.projectNo"
            :label="p.projectName"
            :value="p.projectNo"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="功能" prop="projectFunNo">
        <treeselect style="width:200px; height:30px" v-model="queryParams.projectFunNo" :options="funList" @select="getProjectPointListInfo" :show-count="true" placeholder="请选择功能" />
      </el-form-item>
      <el-form-item label="测试点" prop="projectFunPointNo">
        <el-select v-model="queryParams.projectFunPointNo" placeholder="请选择测试点" clearable>
          <el-option
            v-for="p in pointList"
            :key="p.projectFunPointNo"
            :label="'['+p.projectFunPointPath+']' + p.projectFunPointDesc"
            :value="p.projectFunPointNo"
          >
          <div>{{ p.projectFunPointPath }}</div>
          <div style="color: #8492a6; font-size: 13px">{{ p.projectFunPointDesc }}</div>
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="用例编号" prop="caseNo">
        <el-input
          v-model="queryParams.caseNo"
          placeholder="请输入用例编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用例标题" prop="caseTitle">
        <el-input
          v-model="queryParams.caseTitle"
          placeholder="请输入用例标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="caseType">
        <el-select v-model="queryParams.caseType" placeholder="请选择用例类型" clearable>
          <el-option
            v-for="dict in dict.type.case_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="难度" prop="caseDiffLevel">
        <el-select v-model="queryParams.caseDiffLevel" placeholder="请选择用例难度" clearable>
          <el-option
            v-for="dict in dict.type.case_diff_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="casePriority">
        <el-select v-model="queryParams.casePriority" placeholder="请选择用例优先级" clearable>
          <el-option
            v-for="dict in dict.type.case_priority"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="caseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
      <template slot-scope="scope">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="项目">
            <span>{{ scope.row.projectName }}</span>
          </el-form-item>
          <el-form-item label="项目类型">
            <span>{{ scope.row.projectTypeName }}</span>
          </el-form-item>
          <el-form-item label="功能">
            <span>{{ scope.row.funName }}</span>
          </el-form-item>
          <el-form-item label="功能路径">
            <span>{{ scope.row.pointName }}</span>
          </el-form-item>
          <el-form-item label="用例标题" :show-overflow-tooltip="true">
            <span>{{ scope.row.caseTitle }}</span>
          </el-form-item>
          <el-form-item label="测试点类型">
            <span>{{ scope.row.pointTypeName }}</span>
          </el-form-item>
          
          <el-form-item label="用例类型">
           <span>{{ scope.row.caseTypeName }}</span>
          </el-form-item>
          <el-form-item label="用例难度" >
            <span>{{ scope.row.caseDiffLevelName }}</span>
          </el-form-item>
          <el-form-item label="用例优先级">
            <span>{{ scope.row.casePriorityName }}</span>
          </el-form-item>
          <el-form-item label="前置操作">
             <span v-html="scope.row.beforeStepDesc"></span>
          </el-form-item>
          <el-form-item label="操作步骤">
            <span v-html="scope.row.stepDesc"></span>
          </el-form-item>
          <el-form-item label="预期结果">
            <span v-html="scope.row.acceptResultDesc"></span>
          </el-form-item>
        </el-form>
      </template>
      </el-table-column>
      <el-table-column label="编号" align="center" prop="caseNo" />
      <el-table-column label="标题" :show-overflow-tooltip="true" align="center" prop="caseTitle" />
      <el-table-column label="类型" align="center" prop="caseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.case_type" :value="scope.row.caseType"/>
        </template>
      </el-table-column>
      <el-table-column label="难度" align="center" prop="caseDiffLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.case_diff_level" :value="scope.row.caseDiffLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="优先级" align="center" prop="casePriority">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.case_priority" :value="scope.row.casePriority"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createName" />
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-time"
            @click="handleHistory(scope.row)"  
          >历史记录</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="caseTitle">
          <el-input v-model="form.caseTitle" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="类型" prop="caseType">
          <el-select v-model="form.caseType" placeholder="请选择用例类型">
            <el-option
              v-for="dict in dict.type.case_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="难度" prop="caseDiffLevel">
          <el-select v-model="form.caseDiffLevel" placeholder="请选择用例难度">
            <el-option
              v-for="dict in dict.type.case_diff_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优先级" prop="casePriority">
          <el-select v-model="form.casePriority" placeholder="请选择用例优先级">
            <el-option
              v-for="dict in dict.type.case_priority"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="前置操作" prop="beforeStepDesc">
          <el-input v-model="form.beforeStepDesc" type="textarea" :placeholder="templateInfo" />
        </el-form-item>
        <el-form-item label="操作步骤" prop="stepDesc">
          <el-input v-model="form.stepDesc" type="textarea" :placeholder="templateInfo" />
        </el-form-item>
        <el-form-item label="预期结果" prop="acceptResultDesc">
          <el-input v-model="form.acceptResultDesc" type="textarea" :placeholder="templateInfo" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="historyData.title" :visible.sync="historyData.open" width="850px" append-to-body>
      <div class="block">
        <el-table
            :data="historyData.data"
            style="width: 100%">
            <el-table-column
              prop="caseTitle"
              label="标题"
              width="180">
            </el-table-column>
            <el-table-column
              prop="testPointTypeName"
              label="测试点类型"
              width="100">
            </el-table-column>
            <el-table-column
              prop="caseTypeName"
              label="用例类型">
            </el-table-column>

            <el-table-column
              prop="caseDiffLevelName"
              label="用例难度">
            </el-table-column>

            <el-table-column
              prop="casePriorityName"
              label="优先级">
            </el-table-column>
            <el-table-column
              prop="beforeStep"
              label="前置操作">
            </el-table-column>
            <el-table-column
              prop="step"
              label="操作步骤">
            </el-table-column>
             <el-table-column
              prop="accessResult"
              label="预期结果">
            </el-table-column>
          </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCase, getCase, delCase, addCase, updateCase, getProjectList, listFun, getPointList, case_history } from "@/api/ai_smart/case";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Case",
  dicts: ['case_type', 'case_diff_level', 'case_priority'],
  components: { Treeselect },
  data() {
    return {
      // 历史记录查看
      historyData:{
        title: "历史记录",
        open: false,
        data: [],
      },
      pointList: [],
      funList: [],
      projectList: [],
      templateInfo: `1,xxxxxxxxx
2,xxxxxxxxx
3,xxxxxxxxx
…………`,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用例表格数据
      caseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectNo: null,
        projectFunNo: null,
        projectFunPointNo: null,
        caseNo: null,
        caseTitle: null,
        caseType: null,
        caseDiffLevel: null,
        casePriority: null,
        createDate: null,
        isDeleted: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectNo: [
          { required: true, message: "项目编号不能为空", trigger: "blur" }
        ],
        projectFunNo: [
          { required: true, message: "项目功能编号不能为空", trigger: "blur" }
        ],
        projectFunPointNo: [
          { required: true, message: "项目功能测试点编号不能为空", trigger: "blur" }
        ],
        caseNo: [
          { required: true, message: "用例编号不能为空", trigger: "blur" }
        ],
        caseTitle: [
          { required: true, message: "用例名称不能为空", trigger: "blur" }
        ],
        caseType: [
          { required: true, message: "用例类型不能为空", trigger: "change" }
        ],
        caseDiffLevel: [
          { required: true, message: "用例难度不能为空", trigger: "change" }
        ],
        casePriority: [
          { required: true, message: "用例优先级不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    
    this.queryParams.projectNo = this.$route.query && this.$route.query.projectNo;
    this.getProjectListInfo();
    this.queryParams.projectFunNo = this.$route.query && this.$route.query.projectFunNo;
    if(this.queryParams.projectFunNo){
      this.getFunList();
    }

    this.queryParams.projectFunPointNo = this.$route.query && this.$route.query.projectFunPointNo;
    if(this.queryParams.projectFunPointNo){
      this.getProjectPointListInfo();
    }
    this.getList();
    
  },
  activated(){
    
    this.queryParams.projectNo = this.$route.query && this.$route.query.projectNo;
    this.getProjectListInfo();

    this.queryParams.projectFunNo = this.$route.query && this.$route.query.projectFunNo;
    if(this.queryParams.projectFunNo){
      this.getFunList();
    }
    this.queryParams.projectFunPointNo = this.$route.query && this.$route.query.projectFunPointNo;
    if(this.queryParams.projectFunPointNo){
      this.getProjectPointListInfo();
    }
    this.getList();
    
  },
  methods: {

    // 查看历史记录
    handleHistory(row){
      this.historyData.open = true;
      case_history(row.projectFunPointNo).then(res=>{
        this.historyData.data = res.data;
      });
    },

    /** 查询用例列表 */
    getList() {
      this.loading = true;
      listCase(this.queryParams).then(response => {
        this.caseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectNo: null,
        projectFunNo: null,
        projectFunPointNo: null,
        caseNo: null,
        caseTitle: null,
        caseType: null,
        caseDiffLevel: null,
        casePriority: null,
        createDate: null,
        updateDate: null,
        createId: null,
        updateId: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.caseNo)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用例";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.caseNo || this.ids
      getCase(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用例";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.caseNo || this.ids;
      this.$modal.confirm('是否确认删除用例编号为"' + ids + '"的数据项？').then(function() {
        return delCase(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ai_smart/case/export', {
        ...this.queryParams
      }, `case_${new Date().getTime()}.xlsx`)
    },
    /**
    查询条件的项目列表
    */
    getProjectListInfo(){
      getProjectList().then(res => {
          this.projectList = res.data;
        }).catch(()=>{
          this.projectList = [];
        });
    },
    getFunList(){
      if(!this.queryParams.projectNo){
        this.funList = [];
        return ;
      }
      listFun(this.queryParams.projectNo).then(response => {
        this.funList = response.data;
      });
    },
    getProjectPointListInfo(node){
      let  projectFunNo = "";
      if(node != undefined && node.projectFunNo){
        projectFunNo = node.projectFunNo;
      }else{
        projectFunNo = this.queryParams.projectFunNo;
      }
      getPointList(projectFunNo).then(res => {
          this.pointList = res.data;
        }).catch(()=>{
          this.pointList = [];
        });
    },
    
  }
};
</script>
<style scoped></style>
<style>
  
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
.el-select-dropdown__item {
    font-size: 14px;
    padding: 0 20px;
    position: relative;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    height: auto;
    color: #606266;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    cursor: pointer;
}
</style>